package com.fili.gui.components.table;

import com.fili.gui.components.Bevel;
import com.fili.gui.components.date_time.DateChooser;
import com.fili.gui.components.date_time.TimeChooser;
import com.fili.core.params.DryConfig;
import com.fili.core.params.DryParams;
import com.fili.core.storage.DataStore;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JCalendar;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.Calendar;
import java.io.File;
import java.io.IOException;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 29, 2006
 * Time: 1:51:42 PM
 */
public class TableBottomPanel extends JPanel implements ActionListener
{
    private JButton btnRefresh;
    private JTextField edDBPath;
    private JButton btnBrowse;
    private JComboBox cbInterval;

    private JCheckBox cbEndDate;
    private JRadioButton rbAll;
    private JRadioButton rbStartDate;
    private JDateChooser dtStartDate;
    private TimeChooser dtStartTime;
    private JDateChooser dtEndDate;
    private TimeChooser dtEndTime;

    private DataStore dataStore;
    private JFileChooser fileChooser;

    public TableBottomPanel(DataStore dataStore)
    {
        this.dataStore = dataStore;

        setPreferredSize(new Dimension(617, 97));
        setLayout(null);

        JCalendar.setDefaultLocale(new Locale("RO", "RO"));

        Bevel bvl = new Bevel();
        bvl.setBounds(8, 48, 653, 10);
        add(bvl);

        JLabel lbl = new JLabel("Baza de date");
        lbl.setBounds(16, 67, 81, 13);
        add(lbl);

        lbl = new JLabel("Interval salvare");
        lbl.setBounds(490, 68, 89, 13);
        add(lbl);

        cbEndDate = new JCheckBox("Pana la");
        cbEndDate.setBounds(310, 27, 65, 17);
        cbEndDate.setOpaque(false);
        cbEndDate.addActionListener(this);
        add(cbEndDate);

        rbAll = new JRadioButton("Toate", true);
        rbAll.setBounds(8, 8, 90, 17);
        rbAll.setOpaque(false);
        rbAll.addActionListener(this);
        add(rbAll);

        rbStartDate = new JRadioButton("Incepand cu");
        rbStartDate.setBounds(8, 27, 90, 17);
        rbStartDate.setOpaque(false);
        rbStartDate.addActionListener(this);
        add(rbStartDate);

        ButtonGroup bg = new ButtonGroup();
        bg.add(rbAll);
        bg.add(rbStartDate);

        dtStartDate = new JDateChooser();
        dtStartDate.setLocale(new Locale("RO", "RO"));
        dtStartDate.setDateFormatString("dd / MMM / yyyy");
        dtStartDate.setBounds(103, 25, 128, 21);
        add(dtStartDate);

        dtStartTime = new TimeChooser();
        dtStartTime.setBounds(238, 25, 64, 21);
        add(dtStartTime);

        dtEndDate = new JDateChooser();
        dtEndDate.setLocale(new Locale("RO", "RO"));
        dtEndDate.setDateFormatString("dd / MMM / yyyy");
        dtEndDate.setBounds(381, 25, 128, 21);
        add(dtEndDate);

        dtEndTime = new TimeChooser();
        dtEndTime.setBounds(516, 25, 64, 21);
        add(dtEndTime);

        btnRefresh = new JButton("Actualizare");
        btnRefresh.addActionListener(this);
        btnRefresh.setMargin(new Insets(0, 0, 0, 0));
        btnRefresh.setBounds(588, 24, 73, 22);
        add(btnRefresh);

        edDBPath = new JTextField(DryParams.getParams().getStorePath(dataStore.getID()));
        edDBPath.setBounds(88, 64, 299, 21);
        add(edDBPath);
        edDBPath.setEnabled(!DryConfig.getConfig().isDemoMode());

        btnBrowse = new JButton("Cautare...");
        btnBrowse.addActionListener(this);
        btnBrowse.setMargin(new Insets(0, 0, 0, 0));
        btnBrowse.setBounds(394, 64, 75, 22);
        add(btnBrowse);
        btnBrowse.setEnabled(!DryConfig.getConfig().isDemoMode());

        cbInterval = new JComboBox(new ListItem[]{
                new ListItem(1),
                new ListItem(5),
                new ListItem(10),
                new ListItem(15),
                new ListItem(20),
                new ListItem(30),
                new ListItem(45),
                new ListItem(60),
                new ListItem(90),
                new ListItem(2 * 60),
                new ListItem(3 * 60),
                new ListItem(4 * 60),
                new ListItem(6 * 60),
                new ListItem(8 * 60),
                new ListItem(12 * 60),
                new ListItem(24 * 60),
        });
        cbInterval.setBounds(580, 64, 81, 21);
        add(cbInterval);

        fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new FileFilter()
        {
            public boolean accept(File f)
            {
                return f.getName().endsWith(".bdu");
            }

            public String getDescription()
            {
                return "(*.bdu) Baza Date Uscator";
            }
        });

        updateButtons();
    }

    private void updateButtons()
    {
        if (rbAll.isSelected())
        {
            dtStartDate.setEnabled(false);
            dtStartTime.setEnabled(false);
            dtEndDate.setEnabled(false);
            dtEndTime.setEnabled(false);

            cbEndDate.setEnabled(false);
        }
        else
        {
            dtStartDate.setEnabled(true);
            dtStartTime.setEnabled(true);
            cbEndDate.setEnabled(true);

            dtEndDate.setEnabled(cbEndDate.isSelected());
            dtEndTime.setEnabled(cbEndDate.isSelected());
        }
    }

    public void actionPerformed(ActionEvent e)
    {
        if (btnBrowse.equals(e.getSource()))
        {
            File f = new File(edDBPath.getText());

            fileChooser.setCurrentDirectory(f);
            fileChooser.setSelectedFile(f);
            if (fileChooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
            {
                try
                {
                    edDBPath.setText(fileChooser.getSelectedFile().getCanonicalPath().trim());
                    DryParams.getParams().setStorePath(dataStore.getID(),fileChooser.getSelectedFile().getCanonicalPath().trim());
                }
                catch (IOException e1)
                {
                    e1.printStackTrace();
                }
            }
        }
        updateButtons();
    }

    class ListItem
    {
        private int value;

        public ListItem(int value)
        {
            this.value = value;
        }

        public String toString()
        {
            if (value < 60)
                return value + " min";

            if (value == 60)
                return "1 ora";

            if (value % 60 == 0)
                return (value / 60) + " ore";

            return (value / 60.0) + " ore";
        }
    }
}

